package com.neouton.crm.managerPayRecord.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.managerPayRecord.entity.ManagerPayRecord;
import com.neouton.crm.managerPayRecord.service.ManagerPayRecordService;
import com.neouton.crm.managerPayRequest.entity.ManagerPayRequest;
import com.neouton.crm.managerPayRequest.service.ManagerPayRequestService;
import com.neouton.crm.payRecords.entity.PaymentRecords;
import com.neouton.crm.pm.service.ProjectMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/managerPayRecord")
public class ManagerPayRecordController {

    @Resource
    private ManagerPayRecordService managerPayRecordService;

    @Resource
    private ManagerPayRequestService managerPayRequestService;

    @Resource
    private ProjectMemberService projectMemberService;


    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryManagerPaymentRecordsList/{pageNum}/{pageSize}")
    public ResponseAPI queryManagerPaymentRecordsList(@PathVariable(value = "pageNum") Integer pageNum,
                                                  @PathVariable(value = "pageSize") Integer pageSize,
                                                  @RequestBody ManagerPayRecord managerPayRecord,
                                                  @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"managerPayRecord", token);
        IPage<ManagerPayRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPayRecord> list = managerPayRecordService.queryManagerPaymentRecordsList(page, managerPayRecord, userList, token);
        return ResponseAPI.successData(list);
    }




    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryManagerPaymentRecordsApprovalList/{pageNum}/{pageSize}")
    public ResponseAPI queryManagerPaymentRecordsApprovalList(@PathVariable(value = "pageNum") Integer pageNum,
                                                      @PathVariable(value = "pageSize") Integer pageSize,
                                                      @RequestBody ManagerPayRecord managerPayRecord,
                                                      @RequestHeader("token") String token) {

        IPage<ManagerPayRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPayRecord> list = managerPayRecordService.queryManagerPaymentRecordsApprovalList(page, managerPayRecord, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询付款记录
     * @param entity
     * @return
     */
    @PostMapping("/queryManagerPayRecordedMoney/")
    public ResponseAPI queryManagerPayRecordedMoney(@RequestBody ManagerPayRecord entity) {
        List<ManagerPayRecord> list =  managerPayRecordService.queryManagerPayRecordedMoney(entity);
        return ResponseAPI.successData(list);
    }


    @PostMapping("/saveManagePaymentRecord")
    public ResponseAPI saveManagePaymentRecord(@RequestBody ManagerPayRecord entity,@RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRecordService.saveManagePaymentRecord(entity,token));
    }

    @PostMapping("/approvedManagerPayRecord/")
    public ResponseAPI approvedManagerPayRecord(@RequestBody ManagerPayRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRecordService.approvedManagerPayRecord(entity,token,false));
    }

    @PostMapping("/rejectionManagerPayRecord")
    public ResponseAPI rejectionManagerPayRequest(@RequestBody ManagerPayRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(managerPayRecordService.rejectionManagerPayRecord(entity,token));
    }

    @PostMapping("/queryManagerPayRecordHistory/{approvalId}")
    public ResponseAPI queryManagerPayRecordHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(managerPayRecordService.queryManagerPayRecordHistory(approvalId));
    }

    @PostMapping("/deletePayRecordsById/{payRecordsId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRecordsId") Integer payRecordsId) {
        Boolean flag= managerPayRecordService.deletePayRecordsById(payRecordsId);
        return ResponseAPI.successData(flag);
    }


    @PostMapping("/updatePrIsNotById/{payRequestId}")
    public ResponseAPI updatePrIsNotById(@PathVariable(value = "payRequestId") Integer payRequestId) {
        managerPayRequestService.updatePrIsNotById(payRequestId);
        return ResponseAPI.success();
    }
    /**
     * Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody ManagerPayRecord entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"managerPayRecord", token);
        List<ManagerPayRecord> list = managerPayRecordService.queryPaymentRecordsListInfo(entity, userList, token);
        managerPayRecordService.exportPaymentRecordsInfo(response,list,token);
    }

}
